package com.soulmate.web.order;

import com.soulmate.exception.BusinessException;
import com.soulmate.order.api.OrderService;
import com.soulmate.order.dto.OrderDTO;
import com.soulmate.web.order.param.OrderParam;
import com.soulmate.web.order.vo.OrderVO;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * 订单控制器
 *
 * @author huaiping
 */
@RestController
@RequestMapping("/order")
@AllArgsConstructor
public class OrderController {
    private OrderService orderService;

    /**
     * 创建订单
     *
     * @param orderParam 订单参数
     * @return {@link ResponseEntity}<{@link Object}>
     */
    @PostMapping("/create")
    public ResponseEntity<Object> create(@RequestBody OrderParam orderParam) {
        OrderDTO orderDTO = OrderDTO.builder()
                .productId(orderParam.getProductId())
                .productQuantity(orderParam.getProductQuantity())
                .orderAmount(orderParam.getOrderAmount())
                .build();
        try {
            OrderDTO createOrder = orderService.createOrder(orderDTO);
            OrderVO orderVO = OrderVO.buildVO(createOrder);
            return ResponseEntity.ok(orderVO);
        } catch (BusinessException e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                    .body(e.getMessage());
        }
    }

    /**
     * 订单信息
     *
     * @param orderId 订单id
     * @return {@link ResponseEntity}<{@link OrderVO}>
     */
    @GetMapping("/detail/{orderId}")
    public ResponseEntity<OrderVO> detail(@PathVariable String orderId) {
        OrderDTO orderDTO = orderService.getOrderById(orderId);
        OrderVO orderVO = OrderVO.buildVO(orderDTO);
        return ResponseEntity.ok(orderVO);
    }
}
